package com.example.rent.service.impl;

import com.baomidou.mybatisplus.core.conditions.query.QueryWrapper;
import com.example.rent.entity.House;
import com.example.rent.entity.Houseowner;
import com.example.rent.entity.Meeting;
import com.example.rent.mapper.HouseMapper;
import com.example.rent.mapper.yx.HouseownerMapper;
import com.example.rent.service.HouseownerService;
import com.baomidou.mybatisplus.extension.service.impl.ServiceImpl;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;

import java.sql.Wrapper;
import java.text.SimpleDateFormat;
import java.util.Collections;
import java.util.Comparator;
import java.util.List;

/**
 * <p>
 *  服务实现类
 * </p>
 *
 * @author wanted
 * @since 2021-12-02
 */
@Service
public class HouseownerServiceImpl extends ServiceImpl<HouseownerMapper, Houseowner> implements HouseownerService {

    @Autowired
    HouseownerMapper houseownerMapper;
    @Override
    public List<House> showMyHouse(long id) {
        List<House> houseownerList = houseownerMapper.showMyHouse(id);
        for (int i = 0;i < houseownerList.size();i++){
            int p = i + 1;
            houseownerList.get(i).setHouseid(p);
        }
        return houseownerList;
    }

    public int changePwd(long id,String pwd){
        int i = houseownerMapper.changePwd(id,pwd);
        return i;
    }

    @Override
    public int updateMyInfo(long id, String myName, String address, String freetime) {
        int i = houseownerMapper.updateMyInfo(id,myName,address,freetime);
        return i;
    }

    @Override
    public List<Meeting> getMessage(long id) {
        List<Meeting> meetingList = houseownerMapper.getMessage(id);
        Collections.sort(meetingList, new Comparator<Meeting>() {
            @Override
            public int compare(Meeting o1, Meeting o2) {
                int flag = o1.getDate().compareTo(o2.getDate());
                return flag;
            }
        });
        List<House> houses = houseownerMapper.showMyHouse(id);
        for (int i = 0;i < meetingList.size();i++){
            for (int j = 0;j < houses.size();j++){
                if(houses.get(j).getHouseid() == meetingList.get(i).getHouseid()){
                    meetingList.get(i).setHouseid(meetingList.get(i).getHouseid()*10+houseownerMapper.showMyHouse(id).get(j).getType());
                }
            }

        }
        return meetingList;
    }

    public String getHouseAddress(int houseid){
        String houseAddress = houseownerMapper.getHouseAddress(houseid);
        return  houseAddress;
    }

    @Override
    public int updateHouseState(String address, int state) {
        int i = houseownerMapper.updateHouseState(address,state);
        return i;
    }

    @Override
    public int getHouseId(String address) {
        int i = houseownerMapper.getHouseId(address);
        return i;
    }

    @Override
    public int insertMyHouseInfo(long id, int type, int livenum, float rent, String address, int state) {
        int i = houseownerMapper.insertMyHouseInfo(id,type,livenum,rent,address,state);
        return i;
    }


    @Override
    public int insertRepair(int houseid, String description) {
        int i = houseownerMapper.insertRepair(houseid,description);
        return i;
    }

    @Override
    public int deleteThisHouse(long id, String address, int livenum) {
        int i = houseownerMapper.deleteThisHouse(id,address,livenum);
        return i;
    }
}
